package pkg;
import java.awt.*;
import java.awt.geom.*;


/**
 * @author Andy
 */
public class Player extends Entity {
	
	protected Direction curDirection;
	protected int speed;
	protected int maxSpeed;
	protected int userID = 1; //used in networking for identification
	
	public Player()
	{
		speed = 3;
		maxSpeed = 10;
		setSize(60,60);
		this.bounds = new Rectangle(0,0,60,60);
		collider = new PlayerCollider(this);
		collider.<Player>register(Player.class, "onPlayerHit");
		texture = ResourceManager.getManager().load("Resources/fronts/front1.png");
		curDirection = Direction.Down;
	}
	
	public void onPlayerHit(Player player)
	{
		//nothing to handle currently
	}
	
	
	/**
	 * Moves player in specified direction at max speed.
	 * The player will continue to move until stop() is called.
	 * @param direction Up,Down,Left,Right
	 */
	public void move(Direction direction)
	{
		move(direction,maxSpeed);
	}
	public void move(Direction direction,int speed)
	{
		this.speed = speed > maxSpeed? maxSpeed:speed;
		
		if(direction == Direction.Left)
			velocity = new Point2D.Double(-1,0);
		else if(direction == Direction.Right)
			velocity = new Point2D.Double(1,0);
		else if(direction == Direction.Up)
			velocity = new Point2D.Double(0,-1);
		else if(direction == Direction.Down)
			velocity = new Point2D.Double(0,1);
	
		curDirection = direction;
	}
	
	public void stop()
	{
		speed = 0;
	}
	
	public void update(float dt)
	{
		if(!collider.checkCollisions())
		{
			getPosition().x = (int)Math.round(getPosition().x + this.velocity.getX() * (speed * dt));
			getPosition().y = (int)Math.round(getPosition().y + this.velocity.getY() * (speed * dt));
			//System.out.println( this.getVelocity().x + " And " + this.getVelocity().y);
		}
	}
	
	public void draw(Graphics2D g)
	{
		super.draw(g);
	}
	
	public int getSpeed()
	{
		return speed;
	}
	
	public int getUserID()
	{
		return userID;
	}
	
	public void setUserID(int id)
	{
		userID = id;
	}
	
	public Direction getDirection()
	{
		return this.curDirection;
	}
	
	public void setDirection(Direction set)
	{
		curDirection = set;
	}
	
	/**
	 * Class checks for collisions for moving player objects.
	 *  One bug I know of is that after the collision the player
	 *  does not move to the closest point that it can move too. 
	 *  Although this is only for high speeds so we shouldnt have
	 *  to worry about it.
	 * @author Andy
	 *
	 */
	class PlayerCollider extends Collider<Player>
	{
		public PlayerCollider(Player owner)
		{
			super(owner);
		}
		
		public boolean checkCollisions()
		{
			Rectangle moveBounds = getMoveRectangle();
			java.util.List<Entity> colliders = null;
			
			colliders = owner.getManager().getEntitys(EntityManager.Filters.allWithin(moveBounds));
			
			for(Entity entity : colliders)
				if(owner != entity)
				{
				  entity.collider.fireEvent(owner, entity); //notify other party member
				  this.fireEvent(owner, entity);
				}

			return (colliders.size() > 1); //your self will be included so greater than 1
		}
		/**
		 * Retrieves a rectangle encompassing where the player started to the place where it will end up.
		 */
		private Rectangle getMoveRectangle()
		{
			
			Point nextPos = new Point();
			nextPos.x = (int)(owner.getPosition().x + owner.getVelocity().x * owner.getSpeed());
			nextPos.y = (int)(owner.getPosition().y + owner.getVelocity().y * owner.getSpeed());
			
			Rectangle moveBounds = new Rectangle();
		
			int minX = Math.min(nextPos.x, owner.getPosition().x);
			int minY = Math.min(nextPos.y, owner.getPosition().y);
			int maxX = Math.max(nextPos.x, owner.getPosition().x);
			int maxY = Math.max(nextPos.y, owner.getPosition().y);
			maxX += owner.getSize().x;
			maxY += owner.getSize().y;
			moveBounds.x = minX;
			moveBounds.y = minY;
			moveBounds.width = maxX - minX;
			moveBounds.height = maxY - minY;
			return moveBounds;
		}
	}
	
	public enum Direction
	{
		Left,
		Right,
		Up,
		Down,
	}


}
